<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="采购单名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入采购单名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in bus_purchase_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['crm:purchaseOrder:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseOrderList" style="height: calc(100vh - 280px);" @selection-change="handleSelectionChange">
      <el-table-column type="selection" fixed width="55" align="center" />
      <el-table-column label="ID" fixed align="center" prop="id" width="180"/>
      <el-table-column label="采购单" align="center" fixed prop="name" width="180">
		  <template #default="scope">
			 <el-link type="primary" @click="openPurchaseGoodsWin(scope.row)">{{scope.row.name}}</el-link>
		  </template>
	   </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier" width="180">
        <template #default="scope">
          <dict-tag :options="bus_supplier" :value="scope.row.supplier"/>
        </template>
      </el-table-column>
      <el-table-column label="下单门店" align="center" prop="belDeptId" width="180"/>
	  <el-table-column label="下单人" align="center" prop="downBy" />
	  <el-table-column label="下单时间" align="center" prop="downTime" width="180">
	    <template #default="scope">
	      <span>{{ parseTime(scope.row.downTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
	    </template>
	  </el-table-column>
      <el-table-column label="审核员" align="center" prop="auditorBy" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="验收人" align="center" prop="accepterBy" />
      <el-table-column label="验收时间" align="center" prop="accepterTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.accepterTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库人" align="center" prop="putStoreBy" />
      <el-table-column label="入库时间" align="center" prop="putStoreTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.putStoreTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="进货总量" align="center" prop="total" />
      <el-table-column label="赠送总量" align="center" prop="donate" />
      <el-table-column label="进货总价" align="center" prop="amount" />
      <el-table-column label="实际进货总量" align="center" prop="realityTotal" />
      <el-table-column label="实际赠送总量" align="center" prop="realityDonate" />
      <el-table-column label="实际进货总价" align="center" prop="realityAmount" /> -->
      <el-table-column label="备注" align="center" width="180" :show-overflow-tooltip="true" prop="remark" />
	  <el-table-column label="状态" fixed="right" align="center" prop="status">
	    <template #default="scope">
	      <dict-tag :options="bus_purchase_status" :value="scope.row.status"/>
	    </template>
	  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" v-if="scope.row.status=='1'" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['crm:purchaseOrder:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="purchaseOrderRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="采购单" prop="name">
          <el-input v-model="form.name" placeholder="请输入采购单名称" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier" >
          <el-select v-model="form.supplier" placeholder="请选择供应商" style="width: 100%;">
            <el-option
              v-for="dict in bus_supplier"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea"  v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
	
	<purchaseGoods ref="purchaseGoodsRef" @closed="getList()"/>
  </div>
</template>

<script setup name="PurchaseOrder">
import { onMounted, provide, ref } from "vue";
import { defineStore } from 'pinia'
import { listPurchaseOrder, getPurchaseOrder, delPurchaseOrder, addPurchaseOrder, updatePurchaseOrder,setDownOrder } from "@/api/crm/purchaseOrder";

import purchaseGoods from "../purchaseGoods/index.vue"


const { proxy } = getCurrentInstance();
const { bus_supplier, bus_purchase_status } = proxy.useDict('bus_supplier', 'bus_purchase_status');

const purchaseOrderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const purchaseGoodsRef = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    supplier: null,
    belDeptId: null,
    auditorBy: null,
    auditorTime: null,
    accepterBy: null,
    accepterTime: null,
    putStoreBy: null,
    putStoreTime: null,
    total: null,
    donate: null,
    amount: null,
    realityTotal: null,
    realityDonate: null,
    realityAmount: null,
    status: null,
  },
  rules: {
	  name: [{
	  	required: true,
	  	message: '请输采购单名称',
	  	trigger: 'blur'
	  }],
	  supplier: [{
	  	required: true,
	  	message: '请选择供应商',
	  	trigger: 'change'
	  }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询采购单列表 */
function getList() {
  loading.value = true;
  listPurchaseOrder(queryParams.value).then(response => {
    purchaseOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


function openPurchaseGoodsWin(row){
	purchaseGoodsRef.value.openWin(row.id);
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    supplier: null,
    belDeptId: null,
    auditorBy: null,
    auditorTime: null,
    accepterBy: null,
    accepterTime: null,
    putStoreBy: null,
    putStoreTime: null,
    total: null,
    donate: null,
    amount: null,
    realityTotal: null,
    realityDonate: null,
    realityAmount: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("purchaseOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加采购单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPurchaseOrder(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改采购单";
  });
}



/** 提交按钮 */
function submitForm() {
  proxy.$refs["purchaseOrderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePurchaseOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPurchaseOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除采购单编号为"' + _ids + '"的数据项？').then(function() {
    return delPurchaseOrder(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('crm/purchaseOrder/export', {
    ...queryParams.value
  }, `purchaseOrder_${new Date().getTime()}.xlsx`)
}

getList();

</script>
